//
//  UgcDetailCummentCell.m
//  ZITHER
//
//  Created by weixin on 2018/9/11.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "UgcDetailCummentCell.h"
#import "Ugc_Detail_VC_WX.h"
#import "UILabel+YBAttributeTextTapAction.h"
#import "Ugc_HomePage_VC_V3.h"
#import "MainFirst_Magazine_VC_WX.h"


#define kGAP 10
#define kAvatar_Size 40

@implementation UgcDetailCummentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.backgroundColor = [UIColor colorWithHex:@"f5f5f5"];

    [self setUI];
    [self setMasonry];
}

-(void)setModelComment:(UgcCommentListModelSub *)modelComment{
    _modelComment = modelComment;
    
//    self.nameUser.text = @"用户回复";
    self.comment.text = modelComment.commentContent;
    [self setNameUser];
    
    
    
    //    self.comment.text = [NSString stringWithFormat:@"%@:%@",modelComment.nickName,modelComment.commentContent];

//    if ([modelComment.userId isEqualToString:kUSER_INFO_MANAGER.userId]) {
//        [self.userDelete mas_updateConstraints:^(MASConstraintMaker *make) {
////            make.bottom.equalTo(self).offset(-20);
//            make.height.mas_equalTo(20);
//        }];
//        self.userDelete.hidden = NO;
//    }else{
//        self.userDelete.hidden = YES;
//        [self.userDelete mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.height.mas_equalTo(0);
////            make.bottom.equalTo(self).offset(-12);
//        }];
//    }
    [self setNeedsLayout];
//    [self configComment];
}


-(NSString*)setNameUser{
    NSString *str  = nil;
    //两种状态 一种自己回复 一种是回复其他人
    if ([self.modelComment.replyUserId isEqualToString:self.modelComment.userId]) {
        //不需要展示被回复人
        str= self.modelComment.nickName;
        
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];
        NSMutableParagraphStyle *muStyle = [[NSMutableParagraphStyle alloc] init];
        
        [text addAttribute:NSParagraphStyleAttributeName value:muStyle range:NSMakeRange(0, text.length)];
        
        [text addAttribute:NSForegroundColorAttributeName
                     value:[UIColor colorWithRed:21/255.0 green:21/255.0 blue:21/255.0 alpha:1/1.0]
                     range:NSMakeRange(0, self.modelComment.nickName.length)];
        [text addAttribute:NSFontAttributeName
                     value:[UIFont fontWithName:@"PingFangSC-Semibold" size:14]
                     range:NSMakeRange(0, self.modelComment.nickName.length)];
        
        self.nameUser.attributedText = text;
        
        
        @weakify(self);
        [self.nameUser yb_addAttributeTapActionWithStrings:@[self.modelComment.nickName] tapClicked:^(NSString *string, NSRange range, NSInteger index) {
            @strongify(self);
            Ugc_HomePage_VC_V3 *vc = [[Ugc_HomePage_VC_V3 alloc]init];
            vc.userName = self.modelComment.nickName;
            vc.userId = self.modelComment.userId;
            vc.userAvatar = self.modelComment.avatar;
            [self.getNav pushViewController:vc animated:YES];
        }];
        //设置是否有点击效果，默认是YES
        self.nameUser.enabledTapEffect = NO;
        
    } else{
        //需要展示回复人
        str= [NSString stringWithFormat:@"%@ 回复 %@",
              self.modelComment.nickName,self.modelComment.replyNickName];

        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];
        NSMutableParagraphStyle *muStyle = [[NSMutableParagraphStyle alloc] init];
        
        [text addAttribute:NSParagraphStyleAttributeName value:muStyle range:NSMakeRange(0, text.length)];

        [text addAttribute:NSForegroundColorAttributeName
                     value:[UIColor colorWithRed:21/255.0 green:21/255.0 blue:21/255.0 alpha:1/1.0]
                     range:NSMakeRange(0, self.modelComment.nickName.length)];
        [text addAttribute:NSFontAttributeName
                     value:[UIFont fontWithName:@"PingFangSC-Semibold" size:14]
                     range:NSMakeRange(0, self.modelComment.nickName.length)];

        [text addAttribute:NSForegroundColorAttributeName
                     value:[UIColor colorWithRed:21/255.0 green:21/255.0 blue:21/255.0 alpha:1/1.0]
                     range:NSMakeRange(self.modelComment.nickName.length+4, self.modelComment.replyNickName.length)];
        [text addAttribute:NSFontAttributeName
                     value:[UIFont fontWithName:@"PingFangSC-Semibold" size:14]
                     range:NSMakeRange(self.modelComment.nickName.length+4, self.modelComment.replyNickName.length)];

        self.nameUser.attributedText = text;


        @weakify(self);
        [self.nameUser yb_addAttributeTapActionWithStrings:@[self.modelComment.nickName,self.modelComment.replyNickName] tapClicked:^(NSString *string, NSRange range, NSInteger index) {
            @strongify(self);
            Ugc_HomePage_VC_V3 *vc = [[Ugc_HomePage_VC_V3 alloc]init];

            if ([string isEqualToString:self.modelComment.nickName]) {
                vc.userName = self.modelComment.nickName;
                vc.userId = self.modelComment.userId;
                vc.userAvatar = self.modelComment.avatar;
            }else{
                vc.userName = self.modelComment.replyNickName;
                vc.userId = self.modelComment.replyUserId;
                vc.userAvatar = self.modelComment.replyAvatar;
            }

            [self.getNav pushViewController:vc animated:YES];
        }];
        //设置是否有点击效果，默认是YES
        self.nameUser.enabledTapEffect = NO;
    }
    return @"";
}

-(void)configComment{
    NSString *str  = nil;
    
    //两种状态 一种自己回复 一种是回复其他人
    if ([self.modelComment.replyUserId isEqualToString:self.modelComment.userId]) {
        //不需要展示回复人
        str= [NSString stringWithFormat:@"%@  %@",
              self.modelComment.nickName,self.modelComment.commentContent];
        
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];
        NSMutableParagraphStyle *muStyle = [[NSMutableParagraphStyle alloc] init];
        
        if ([text.string isMoreThanOneLineWithSize:CGSizeMake(kScreenW-kGAP-kAvatar_Size-2*kGAP, CGFLOAT_MAX) font:[UIFont fontWithName:@"PingFang-SC-Regular" size:13] lineSpaceing:5.0]) {//margin
            muStyle.lineSpacing = 5.0;//设置行间距离
        }else{
            muStyle.lineSpacing = CGFLOAT_MIN;//设置行间距离
        }
        
        [text addAttribute:NSParagraphStyleAttributeName value:muStyle range:NSMakeRange(0, text.length)];
        
        [text addAttribute:NSForegroundColorAttributeName
                     value:[UIColor blackColor]
                     range:NSMakeRange(0, self.modelComment.nickName.length)];
        [text addAttribute:NSFontAttributeName
                     value:[UIFont fontWithName:@"PingFang-SC-Regular" size:13]
                     range:NSMakeRange(0, self.modelComment.nickName.length)];
        
        self.comment.attributedText = text;
        
        
        @weakify(self);
        [self.comment yb_addAttributeTapActionWithStrings:@[self.modelComment.nickName] tapClicked:^(NSString *string, NSRange range, NSInteger index) {
            @strongify(self);
            Ugc_HomePage_VC_V3 *vc = [[Ugc_HomePage_VC_V3 alloc]init];
            vc.userName = self.modelComment.nickName;
            vc.userId = self.modelComment.userId;
            vc.userAvatar = self.modelComment.avatar;
            [self.getNav pushViewController:vc animated:YES];
        }];
        //设置是否有点击效果，默认是YES
        self.comment.enabledTapEffect = NO;

    } else{
        //需要展示回复人
        str= [NSString stringWithFormat:@"%@回复%@ %@",
              self.modelComment.nickName,self.modelComment.replyNickName,self.modelComment.commentContent];
        
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];
        NSMutableParagraphStyle *muStyle = [[NSMutableParagraphStyle alloc] init];
        
        if ([text.string isMoreThanOneLineWithSize:CGSizeMake(kScreenW-kGAP-kAvatar_Size-2*kGAP, CGFLOAT_MAX) font:[UIFont fontWithName:@"PingFang-SC-Regular" size:13] lineSpaceing:5.0]) {//margin
            muStyle.lineSpacing = 5.0;//设置行间距离
        }else{
            muStyle.lineSpacing = CGFLOAT_MIN;//设置行间距离
        }
        
        [text addAttribute:NSParagraphStyleAttributeName value:muStyle range:NSMakeRange(0, text.length)];
        
        [text addAttribute:NSForegroundColorAttributeName
                     value:[UIColor blackColor]
                     range:NSMakeRange(0, self.modelComment.nickName.length)];
        [text addAttribute:NSFontAttributeName
                     value:[UIFont fontWithName:@"PingFang-SC-Regular" size:13]
                     range:NSMakeRange(0, self.modelComment.nickName.length)];
        
        [text addAttribute:NSForegroundColorAttributeName
                     value:[UIColor blackColor]
                     range:NSMakeRange(self.modelComment.nickName.length+2, self.modelComment.replyNickName.length)];
        [text addAttribute:NSFontAttributeName
                     value:[UIFont fontWithName:@"PingFang-SC-Regular" size:13]
                     range:NSMakeRange(self.modelComment.nickName.length+2, self.modelComment.replyNickName.length)];
        
        self.comment.attributedText = text;
        
        
        
        
        @weakify(self);
        [self.comment yb_addAttributeTapActionWithStrings:@[self.modelComment.nickName,self.modelComment.replyNickName] tapClicked:^(NSString *string, NSRange range, NSInteger index) {
            @strongify(self);
            Ugc_HomePage_VC_V3 *vc = [[Ugc_HomePage_VC_V3 alloc]init];

            if ([string isEqualToString:self.modelComment.nickName]) {
                vc.userName = self.modelComment.nickName;
                vc.userId = self.modelComment.userId;
                vc.userAvatar = self.modelComment.avatar;
            }else{
                vc.userName = self.modelComment.replyNickName;
                vc.userId = self.modelComment.replyUserId;
                vc.userAvatar = self.modelComment.replyAvatar;
            }
            
            [self.getNav pushViewController:vc animated:YES];
        }];
        //设置是否有点击效果，默认是YES
        self.comment.enabledTapEffect = NO;
    }

}


-(void)setMasonry{
    
    [self.comment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameUser.mas_bottom).offset(0);
        make.left.equalTo(self).offset(55);
        make.right.equalTo(self).offset(-40);
        make.bottom.equalTo(self).offset(-8);
    }];
    
    [self.nameUser mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(0);
        make.left.equalTo(self).offset(55);
    }];
    
}

-(void)setUI{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW-30, 1)];
    view.backgroundColor = [UIColor colorWithHex:@"F5F5F5"];
    [self addSubview:view];
    [view mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(0);
        make.left.mas_equalTo(self).offset(15);
        make.right.mas_equalTo(self).offset(-15);
        make.bottom.mas_equalTo(self).offset(0);
    }];
    [self addSubview:self.comment];
//    [self addSubview:self.userDelete];
    [self addSubview:self.nameUser];
}

-(QMUILabel *)comment{
    if (!_comment) {
        _comment = [[QMUILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        _comment.numberOfLines = 0;
        _comment.text = @"";
        _comment.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:14];

//        _comment.font = [UIFont fontWithName:@"PingFang-SC-Light" size:14];
        _comment.textColor = [UIColor colorWithHex:@"333333"];
    }
    return _comment;
}

-(BaseLab *)nameUser{
    if (!_nameUser) {
        _nameUser = [[BaseLab alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        _nameUser.numberOfLines = 0;
        _nameUser.text = @"";
        _nameUser.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:14];
        _nameUser.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1/1.0];

    }
    return _nameUser;
}

-(QMUIButton *)userDelete{
    if (!_userDelete) {
        _userDelete = [[QMUIButton alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        [_userDelete setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        _userDelete.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:13];
        [_userDelete setTitle:@"删除" forState:UIControlStateNormal];
        _userDelete.hidden = YES;
        @weakify(self);
        [[_userDelete rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            [[serviceDefualt loadDelUserCommentCommentId:self.modelComment.commentId reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                if (x.success) {
                    
                    if (index == 0) {
                        Ugc_Detail_VC_WX *vc = (Ugc_Detail_VC_WX*)[self getViewController:[Ugc_Detail_VC_WX class]];
                        [vc.tbv.mj_header beginRefreshing];
                        
                    }else{
                        MainFirst_Magazine_VC_WX *vc = (MainFirst_Magazine_VC_WX*)[self getViewController:[MainFirst_Magazine_VC_WX class]];
                        [vc.Tbv Starefreshing];
                    }

                    [Tools showTips:@"删除成功"];
                }else{
                    [Tools showTips:x.errorInfo];
                     
                }
            }];
        }];
    }
    return _userDelete;
}

-(void)setState:(UgcDetailCummentCellState )state{
    _state = state;
    
    switch (state) {
        case UgcDetailCummentCellNormal:{
            [self.comment mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self).offset(14);
                make.left.equalTo(self).offset(30);
                make.right.equalTo(self).offset(-30);
                make.bottom.mas_equalTo(self).offset(0);
            }];
        }
            break;
        case UgcDetailCummentCellFirst:{
            [self.comment mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self).offset(5);
                make.left.equalTo(self).offset(30);
                make.right.equalTo(self).offset(-30);
                make.bottom.mas_equalTo(self).offset(0);
            }];
        }
            break;
        case UgcDetailCummentCellEnd:{
            [self.comment mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self).offset(14);
                make.left.equalTo(self).offset(30);
                make.right.equalTo(self).offset(-30);
                make.bottom.mas_equalTo(self).offset(10);
            }];
        }
            break;
            
        default:
            break;
    }

    
}

@end
